// This MFC Samples source code demonstrates using MFC Microsoft Office Fluent User Interface 
// (the "Fluent UI") and is provided only as referential material to supplement the 
// Microsoft Foundation Classes Reference and related electronic documentation 
// included with the MFC C++ library software.  
// License terms to copy, use or distribute the Fluent UI are available separately.  
// To learn more about our Fluent UI licensing program, please visit 
// http://msdn.microsoft.com/officeui.
//
// Copyright (C) Microsoft Corporation
// All rights reserved.

// wfdeqView.cpp : implementation of the CwfdeqView class
//

/**
 * \file
 * \ingroup View
 * ---
 *	Contains class wfdeqView
 *
*/

/**
 * \ingroup Interface
 * \brief View class for document/View architecture
 * \author HapTork
*/

#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "wfdeq.h"
#endif

#include "wfdeqDoc.h"
#include "wfdeqView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CwfdeqView

IMPLEMENT_DYNCREATE(CwfdeqView, CListView)

BEGIN_MESSAGE_MAP(CwfdeqView, CListView)
	ON_WM_CONTEXTMENU()
	ON_WM_RBUTTONUP()
	ON_COMMAND(ID_TSTBUT, &CwfdeqView::OnTstbut)
	ON_WM_SETFOCUS()
	ON_COMMAND(IDC_DOIT, &CwfdeqView::OnDoit)
END_MESSAGE_MAP()

// CwfdeqView construction/destruction
CWnd* CwfdeqView::flagSetFocus=NULL;
CwfdeqView::CwfdeqView()
	: pdeq(NULL)
	, curColumn(0)
{

}

CwfdeqView::~CwfdeqView()
{
}

BOOL CwfdeqView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	cs.style |= LVS_REPORT;
	return CListView::PreCreateWindow(cs);
}

void CwfdeqView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();

	CListCtrl& wndList = GetListCtrl ();

	wndList.SetExtendedStyle (LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

	wndList.InsertColumn (curColumn++, _T("(x,y)"), LVCFMT_LEFT, 65);
	// TODO: You may populate your ListView with items by directly accessing
	//  its list control through a call to GetListCtrl().
}

void CwfdeqView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void CwfdeqView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}


// CwfdeqView diagnostics

#ifdef _DEBUG
void CwfdeqView::AssertValid() const
{
	CListView::AssertValid();
}

void CwfdeqView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}

CwfdeqDoc* CwfdeqView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CwfdeqDoc)));
	return (CwfdeqDoc*)m_pDocument;
}
#endif //_DEBUG


// CwfdeqView message handlers


void CwfdeqView::OnTstbut()
{
	CString s=_T("(YU,1.5)");
	s=s.Trim();
	CString resToken,show;
	int curPos = 0;
	::AfxMessageBox(s);
	resToken= s.Tokenize(_T("(,)[]"),curPos);
	while (resToken != _T(""))
	{
		float x=_ttof(resToken);
		show.Format(_T("%3.2f"),x);
		::AfxMessageBox(show);
		resToken = s.Tokenize(_T("(,)[]"), curPos);
	};   
	::AfxMessageBox(resToken);

}

void CwfdeqView::OnSetFocus(CWnd* pOldWnd)
{
	CListView::OnSetFocus(pOldWnd);
	if(flagSetFocus!=this){
		flagSetFocus=this;
		GetDocument()->UpdateUI();
	}

}

void CwfdeqView::OnDoit()
{
	CwfdeqDoc* pDoc=GetDocument();
	pdeq=pDoc->getPde();
	if(!pdeq)	return;
	int flag=pDoc->UpdateFD();
	if(!flag)
		return;
	float dx=(float)pdeq->getDx(pDoc->m_nLcmX+1);
	float dy=(float)pdeq->getDy(pDoc->m_nLcmY+1);
	CString item;
	CListCtrl& wndList = GetListCtrl();

	if(flag==2){
		int tColumn=curColumn;
		curColumn=0;
		wndList.DeleteAllItems();
		for(int i=0;i<=pDoc->m_nLcmX;i++)
			for(int j=0;j<=pDoc->m_nLcmY;j++)
			{
				item.Format(_T("(%.2lf,%.2lf)"),dx*i,dy*j);
				wndList.InsertItem (i*(pDoc->m_nLcmX+1)+j, item, curColumn);
			}
		if(pdeq->isExact){
		if(--tColumn>0)	++curColumn; else	wndList.InsertColumn (++curColumn, _T("Analytical Sol"), LVCFMT_LEFT, 75);
			for(int i=0;i<=pDoc->m_nLcmX;i++)
				for(int j=0;j<=pDoc->m_nLcmY;j++){
					item.Format(_T("%.8lf"),pDoc->eGrid->body[i][j]);
					wndList.SetItemText(i*(pDoc->m_nLcmX+1)+j,curColumn, item);
				}
		//	}
		}//same repetition for u too if m_nPdeType>1
		for(int k=0;k<pDoc->soList.GetSize();k++){
			if(--tColumn>0)	++curColumn;	else wndList.InsertColumn (++curColumn,CString(pDoc->soList[k].name), LVCFMT_LEFT, 75);
			int x=pDoc->soList[k].uGrid.getX()-1;
			int y=pDoc->soList[k].uGrid.getY()-1;
			int factX=x/pDoc->m_nLcmX;
			int factY=y/pDoc->m_nLcmY;
			for(int i=0,m=0;m<=x;m+=factX,i++)
				for(int j=0,n=0;n<=y;n+=factY,j++){
					item.Format(_T("%.8lf"),pDoc->soList[k].uGrid.body[m][n]);
					wndList.SetItemText(i*(pDoc->m_nLcmX+1)+j,curColumn, item);
				}
			if(pdeq->isExact){
				if(--tColumn>=0)	++curColumn;	else wndList.InsertColumn (++curColumn,CString("Error ")+CString(pDoc->soList[k].name), LVCFMT_LEFT, 85);
				for(int i=0,m=0;m<=x;m+=factX,i++)
					for(int j=0,n=0;n<=y;n+=factY,j++){
						item.Format(_T("%.10lf"),fabs(pDoc->eGrid->body[i][j]-pDoc->soList[k].uGrid.body[m][n]));
						wndList.SetItemText(i*(pDoc->m_nLcmX+1)+j,curColumn, item);
					}
			}
		}
	}
	else{
		int k=pDoc->soList.GetUpperBound();
		wndList.InsertColumn (++curColumn,CString(pDoc->soList[k].name), LVCFMT_LEFT, 75);
		int x=pDoc->soList[k].uGrid.getX()-1;
		int y=pDoc->soList[k].uGrid.getY()-1;
		int factX=x/pDoc->m_nLcmX;
		int factY=y/pDoc->m_nLcmY;
		for(int i=0,m=0;m<=x;m+=factX,i++)
			for(int j=0,n=0;n<=y;n+=factY,j++){
				item.Format(_T("%.8lf"),pDoc->soList[k].uGrid.body[m][n]);
				wndList.SetItemText(i*(pDoc->m_nLcmX+1)+j,curColumn, item);
			}
		if(pdeq->isExact){
			wndList.InsertColumn (++curColumn,CString("Error ")+CString(pDoc->soList[k].name), LVCFMT_LEFT, 85);
			for(int i=0,m=0;m<=x;m+=factX,i++)
				for(int j=0,n=0;n<=y;n+=factY,j++){
					item.Format(_T("%.10lf"),fabs(pDoc->eGrid->body[i][j]-pDoc->soList[k].uGrid.body[m][n]));
						wndList.SetItemText(i*(pDoc->m_nLcmX+1)+j,curColumn, item);
				}
		}
	}
	//wndList.InsertColumn (curColumn++,CString(GetDocument()->soList[0].name), LVCFMT_LEFT, 70);
	//writeGrid(GetDocument()->soList[0].nGrid,curColumn-1);
	//wndList.InsertColumn (curColumn++, _T("Exact Sol"), LVCFMT_LEFT, 70);
	//writeGrid(*(GetDocument()->eGrid),curColumn-1);
	//wndList.InsertItem (2, _T("heo"), 0);
	//wndList.DeleteColumn(1);
}
